package com.ungao.tools.generator.builder;


import com.ungao.tools.generator.beans.TableField;
import com.ungao.tools.generator.beans.TableInfo;
import com.ungao.tools.generator.connection.IConnection;
import com.ungao.tools.generator.converter.IConverter;
import com.ungao.tools.generator.rules.IColumnType;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TableBuilder extends AbstractBuilder<List<TableInfo>, List<String>, TableBuilder> {

    public TableBuilder(List<String> names, List<String> alias) {
        super(names, alias, new ArrayList<>());
    }

    @Override
    public TableBuilder create(IConnection connection) throws Exception {
        try {
            for (String tableName : super.names) {

                //表信息构造器
                final TableInfo.TableInfoBuilder infoBuilder = TableInfo.builder();

                //获取表信息
                ResultSet tables = connection.tables(tableName);
                while (tables.next()) {
                    infoBuilder.className(tables.getString("TABLE_NAME"));
                    infoBuilder.tableName(tables.getString("TABLE_NAME"));
                    infoBuilder.tableComment(tables.getString("REMARKS"));
                }

                //获取主键信息
                Map<String, String> keymap = new HashMap<>(16);
                ResultSet primaries = connection.primaries(tableName);
                while (primaries.next()) {
                    String columnName = primaries.getString("COLUMN_NAME");
                    String columnKey = primaries.getString("PK_NAME");
                    keymap.putIfAbsent(columnName, columnKey);
                }

                //获取列信息
                List<TableField> tableColumns = new ArrayList<>();
                ResultSet columns = connection.columns(tableName);
                while (columns.next()) {
                    TableField.TableFieldBuilder columnBuilder = TableField.builder();
                    columnBuilder.columnName(columns.getString("COLUMN_NAME"));
                    columnBuilder.fieldName(columns.getString("COLUMN_NAME"));
                    columnBuilder.columnType(columns.getString("TYPE_NAME"));
                    columnBuilder.jdbcType(columns.getString("TYPE_NAME"));
                    columnBuilder.columnLength(columns.getString("COLUMN_SIZE"));
                    columnBuilder.columnComment(columns.getString("REMARKS"));
                    columnBuilder.columnDefault(columns.getString("COLUMN_DEF"));
                    columnBuilder.columnPosition(columns.getString("ORDINAL_POSITION"));
                    columnBuilder.decimalDigits(columns.getString("DECIMAL_DIGITS"));
                    columnBuilder.isNullable(columns.getString("NULLABLE"));
                    columnBuilder.isAutoIncrement(columns.getString("IS_AUTOINCREMENT"));
                    columnBuilder.columnKey(keymap.get(columns.getString("COLUMN_NAME")));
                    tableColumns.add(columnBuilder.build());
                }

                //构建列信息
                super.tables.add(infoBuilder.columns(tableColumns).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            connection.close();
        }
        return this;
    }

    @Override
    public TableBuilder handle(IColumnType columnType) throws Exception {
        for (TableInfo table : super.tables) {

            //处理数据类型
            for (TableField column : table.getColumns()) {
                column.setJdbcType(columnType.getJdbcType(column.getJdbcType()));
                column.setColumnType(columnType.getFieldType(column.getColumnType()));
            }
        }
        return this;
    }

    @Override
    public TableBuilder converter(IConverter converter) throws Exception {
        for (TableInfo table : super.tables) {

            //转换类名名称
            String className = table.getClassName();
            if (super.alias != null && super.alias.size() > 0) {
                for (String alia : super.alias) {
                    if (className.startsWith(alia)) {
                        className = className.substring(alia.length());
                    }
                }
            }
            table.setClassName(converter.toField(converter.toUpperCase(className)));

            //转换属性名称
            for (TableField column : table.getColumns()) {
                String fieldName = column.getFieldName();
                column.setFieldName(converter.toField(fieldName.toLowerCase()));
            }
        }
        return this;
    }

    @Override
    public List<TableInfo> build() {
        return super.tables;
    }
}
